<?php
/*
 * Copyright notice
 * 
 * (c) 2009 MH-Dev.de, Martin Hesse <info@mh-dev.de>
 * 
 * This script is part of the MH-Dev.-Board project. The MH-Dev.-Board 
 * is free software; you can redistribute it and/or modify it under the 
 * terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, 
 * or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU 
 * General Public License along with this program; if not, 
 * see http://www.gnu.org/licenses/.
 */


/**
 * MessagesController
 *
 * @author	Martin Hesse <info@mh-dev.de>
 * @package	MH-Dev.-Board
 */
class MessagesController extends Zend_Controller_Action
{
	private $_userUid; // user uid
	private $_userRole; // user role
	
	public function init()
	{
		// registry
		$this->registry	= Zend_Registry::getInstance();
		
		// auth
		$auth	= Zend_Auth::getInstance();
    	$this->view->auth	= $auth->hasIdentity();

    	if($auth->hasIdentity())
    	{
    		$this->view->auth_role 			= $auth->getIdentity()->role;
    		$this->_userRole				= $auth->getIdentity()->role;
    		$this->view->username 			= $auth->getIdentity()->username;
    		$this->view->auth_uid			= $auth->getIdentity()->uid;
    		$this->_userUid					= $auth->getIdentity()->uid;
    	}
    	else
    	{
    		$this->view->username	= $this->registry->get('lang')->_('guest');
    	}

    	$this->auth	= $auth;
    	
		// lang
		$this->view->translate		= $this->registry->get('lang');
		
		// flash messenger
		$this->view->flashMessenger	= $this->_helper->FlashMessenger;
		
		// include jquery
		$baseurl = new MHDEV_View_Helper_BaseUrl();
		$this->view->headScript()->appendFile($baseurl->BaseUrl() . '/static/css/ui-lightness/jquery-ui-1.7.2.custom.css');
		$this->view->headScript()->appendFile($baseurl->BaseUrl() . '/static/js/jquery-1.3.2.min.js');
		$this->view->headScript()->appendFile($baseurl->BaseUrl() . '/static/js/jquery-ui-1.7.2.custom.min.js');
	}
	
	
	
	/*
	 * List of all messages
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function indexAction()
	{
		// param
		$page 		= intval($this->getRequest()->getParam('page', 0));
		$count		= 15; 
		
		// db
		$messages	= new Messages();
		
		// query
		$query		= $messages->fetchAll('is_deleted = 0 AND receiver_uid =' . $this->_userUid, 'create_date DESC');
		
		// paginator
		$paginator	= Zend_Paginator::factory($query->toArray());
        $paginator->setItemCountPerPage($count);
        $paginator->setCurrentPageNumber($page);
		
		// view
		$this->view->results 	= $paginator;
		$this->view->count		= $query->count();
	}
	
	
	
	/*
	 * List of outgoing messages
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function outboxAction()
	{
		// param
		$page 		= intval($this->getRequest()->getParam('page', 0));
		$count		= 15;
		
		// db
		$messages	= new Messages();
		
		// query
		$query		= $messages->fetchAll('user_uid =' . $this->_userUid, 'create_date DESC');
		
		// paginator
		$paginator	= Zend_Paginator::factory($query->toArray());
        $paginator->setItemCountPerPage($count);
        $paginator->setCurrentPageNumber($page);
		
		// view
		$this->view->results 	= $paginator;
		$this->view->count		= $query->count();
	}
	
	
	
	/*
	 * Read a message ...
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function showAction()
	{
		// param
		$uid 		= (int)$this->getRequest()->getParam('uid', FALSE);
		
		// db
		$messages 	= new Messages();
		
		// query
		$query		= $messages->fetchAll(array(
			'uid =' . $uid,
			'receiver_uid ='.$this->_userUid
		));
		
		// form
		$replyForm	= new ReplyForm();
		
		// view
		$this->view->count 		= $query->count();
		$this->view->results 	= $query->toArray();
		$this->view->replyForm	= $replyForm;
		
		if($query->count() > 0)
		{
			$temp	= $query->toArray();
			
			if($temp[0]['is_read'] == 0)
			{
				$messages->update(array('is_read' => 1), 'uid = ' . $uid);
			}
		}
		
		// get post
		if ($this->_request->isPost())
		{
			$formData = $this->_request->getPost();
			if ($replyForm->isValid($formData)) 
			{
				$row 				= $messages->createRow($replyForm->getValues());
				$row->create_date	= new Zend_Db_Expr('NOW()');
				$row->user_uid		= $this->_userUid;
				$row->save();
			}
			$this->_redirect('/messages.html');
		}
	}
	
	
	
	public function replyAction()
	{
		
	}
	
	
	
	/*
	 * Delete a message
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function deleteAction()
	{
		// disable layout
		Zend_Layout::getMvcInstance()->disableLayout();
		
		// post
		$postData	= $this->_request->getPost();
		
		// secure redirect
		if($postData['uid'] > 0) 
		{
			// db
			$query 		= new Messages();
			
			//query
			$fetch 		= $query->find((int)$postData['uid']);
			
			// check user
			if($fetch->count() > 0)
			{
				$receiver 	= $fetch->current()->__get('receiver_uid');
						
				if($receiver == $this->_userUid OR $this->_userRole == 'admin')
				{
					$query->update(array('is_deleted' => 1),'uid=' . (int)$postData['uid']);
				}
			}
		}
	}
}